package com.qf.ayc.customers.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.ayc.customers.entity.Customers;
import com.qf.ayc.customers.mapper.CustomersMapper;
import  com.qf.ayc.customers.service.ICustomersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.ayc.user.entity.Users;
import com.qf.ayc.user.vo.LoginUserVo;
import com.qf.ayc.user.vo.LoginVo;
import com.qf.ayc.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-02-20
 */
@Service
@Slf4j
public class CustomersServiceImpl extends ServiceImpl<CustomersMapper, Customers> implements ICustomersService {

    @Autowired
    private CustomersMapper customersMapper;
    @Override
    public void add(Customers customers) {
        customersMapper.insert(customers);
    }

    @Override
    public LoginVo login(String username, String password) {
        Customers Auser = customersMapper.selectOne(new QueryWrapper<Customers>().eq("username", username));
        LoginVo loginVo = new LoginVo();
        if(Auser==null){
            log.info("没有找到对应的用户信息，用户名{}错误",username);
            loginVo.setCode(-1);
            loginVo.setMsg("没有找到对应的用户信息，用户名错误");
            return loginVo;
        }

        if(!password.equals(password)){
            log.info("密码错误");
            loginVo.setCode(-2);
            loginVo.setMsg("密码错误");
            return loginVo;
        }
        Integer id = Auser.getId();
        String token = JwtUtils.createToken(username, 120);

        loginVo.setCode(1);
        loginVo.setMsg(username);
        loginVo.setLoginUserVo(new LoginUserVo(username,token,null,null,null,id));
        return loginVo;
    }

    @Override
    public boolean regist(String username, String password) {
        Customers Auser = customersMapper.selectOne(new QueryWrapper<Customers>().eq("username", username));
        System.out.println(Auser+"Auser");
        if(Auser!=null){
            log.info("注册用户时用户名已存在，username={}", username);
            return false;
        }
        Customers customers = new Customers();
        customers.setUsername(username);
        customers.setPassword(password);
        customersMapper.insert(customers);
        return true;
    }


}
